package drawing;

import java.awt.Rectangle;

import javax.swing.JFrame;

public class DrawingEngine {
	private CgPanel panel;
	final String lock = "a";
	JFrame frame;
	/**
	 * Draws starting bottom left to top right, 
	 * @param matrix
	 */
	public void draw(int[][] matrix) {
		
		for (int x = 0; x < matrix.length; x++) {
			for (int y = 0; y < matrix[0].length; y++) {
				panel.drawPixel(x, matrix[0].length-1-y, matrix[x][y]);
			}
		}
	}

	public DrawingEngine(int xmax, int ymax) {
		panel = new CgPanel(xmax,ymax);
		frame = new JFrame();
		frame.getContentPane().add(panel);
		panel.setBounds(new Rectangle(xmax, ymax));
		frame.setBounds(new Rectangle(xmax+50, ymax+50));

	}

	public void go() {
		frame.setVisible(true);
		panel.setVisible(true);
		new Thread(new Runnable() {
			@Override
			public void run() {
				while (true) {
					try {
						synchronized (lock) {
							lock.wait();
						}
					} catch (InterruptedException e) {
						throw new Error("fok alles");
					}
					panel.redraw();
					}
			}
		}).start();
		panel.setVisible(true);
		panel.repaint();
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
		}
	}
	public void notifyChange()
	{
		synchronized (lock) {
			lock.notify();
		}
	}
	public synchronized void setPixel(int x,int y,float[] color){
		panel.drawPixel(x, y, color[0],color[1],color[2]);
	}
	public void draw(float[][][] matrix) {
		for (int y = 0; y < matrix.length; y++) {
		for (int x = 0; x < matrix[0].length; x++) {
			panel.drawPixel(x, y, matrix[y][x][0],matrix[y][x][1],matrix[y][x][2]);
		}
	}
	synchronized (lock) {

		lock.notify();

	}
		
	}

	public void writeImage(String string) {
		panel.saveImage(string);
		
	}

	public void clear() {
		panel.clear();
		notifyChange();
	}

}
